package org.vectrics.recruiter.reporting;



import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import org.apache.log4j.Logger;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.vectrics.user.ServiceRequest;
import org.vectrics.user.ServiceRequestCriteria;
import org.vectrics.user.ServiceRequestSortOrder;
import org.vectrics.VectricsException;
import org.vectrics.common.util.DateUtil;
import org.vectrics.domain.Domain;
import org.vectrics.history.HistoryProvider;
import java.util.Iterator;
import java.util.Locale;
import java.util.Date;


/**
 * 
 * @author mmoore
 */
public class UsageReport extends RecruitBaseReport {
	private transient static Logger log = Logger.getLogger(UsageReport.class);
    
	
	private PdfPTable usageTable = new PdfPTable(6);
	private Date endDate = new Date(); 
	private Date startDate = new Date(endDate.getTime() - (DateUtil.MILLIS_IN_A_DAY * 5)); 
	
	
	public UsageReport(Domain domain, Locale locale) {
		super(domain, locale);
	}

	@Override
	protected Rectangle getPageSize() {
		return(PageSize.A4.rotate());
	}


	@Override
	protected void fill(Document document) throws VectricsException {
		log.debug("fill");
		try {
			
			document.add(new Paragraph("Usage Report    >> " +  CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(startDate)
					+ " to " + CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(this.endDate),
					ReportConstants.titleFont));

			log.debug("fill() - setting table properties");
			usageTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			usageTable.setWidthPercentage(100);
			int usageTableWidths[] = {80, 60, 30, 30, 55, 150};
			usageTable.setWidths(usageTableWidths);

			log.debug("fill() - adding table header row");
			addTableHeaderCell(this.usageTable, "prompt.time.stamp");
			addTableHeaderCell(this.usageTable, "prompt.user.name");
			addTableHeaderCell(this.usageTable, "prompt.country");
			addTableHeaderCell(this.usageTable, "prompt.language");
			addTableHeaderCell(this.usageTable, "prompt.client.ip");
			addTableHeaderCell(this.usageTable, "prompt.request");
			usageTable.setHeaderRows(1);
			
			log.debug("fill() - adding user rows");
			ServiceRequestCriteria criteria = new ServiceRequestCriteria();
			criteria.setDateStart(this.startDate);
			criteria.setDateEnd(this.endDate);
			ServiceRequestSortOrder sort = new ServiceRequestSortOrder(); 
			Iterator iter = HistoryProvider.getInstance().findServiceRequests(criteria, sort, 0, 2000).iterator();


			while (iter.hasNext()) {
				ServiceRequest request = (ServiceRequest)iter.next();

				addHistoryRow(request);
			}
			log.debug("fill() - adding user table to document");
			document.add(usageTable);

		}
		catch(Exception e) {
			log.error("Exception creating pdf document - in fill()", e);
			throw new VectricsException("Exception creating pdf document - fill()", e);
		}
        
	}



    
	protected void setupHeaderFooter(Document document) throws VectricsException {
	}


	protected void addTableHeaderCellCode(String titleCode) {
		String title = CoreServiceLocator.getPhraseProvider().getMessage(titleCode, this.getLocale());
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		this.usageTable.addCell(hdrCell);	
	}
	   

	protected void addHistoryRow(ServiceRequest request) {
		Color color = new Color(255, 255, 255);
		
		PdfPCell cell = new PdfPCell(new Phrase(request.getTimeStamp().toString(), 
					ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	

		Long personId = request.getPersonId();
		if (personId != null) {
			Person person = CoreServiceLocator.getPersonService().findPersonById(personId);
			cell = new PdfPCell(new Phrase(person.getName(), ReportConstants.stdFont));
		} else {
			cell = new PdfPCell(new Phrase(" ", ReportConstants.stdFont));
		}
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(request.getCountry(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(request.getLanguage(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(request.getClientIp(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	

		cell = new PdfPCell(new Phrase(request.getRequest(), ReportConstants.stdFont));
		cell.setBackgroundColor(color);
		this.usageTable.addCell(cell);	
	}   
	
	

	/**
	 * @return
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

}
